package com.artisan.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2017/7/19.
 */
public class DateTimeUtils {
    public static long getAfterMinuteTime(Date date,int minuts){
        return date.getTime()+minuts*60*1000;
    }

    public static String dateToStr(Date date,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static long dateDiff(Date before,Date after){

        long l=after.getTime()-before.getTime();
       /* long day=l/(24*60*60*1000);
        long hour=(l/(60*60*1000)-day*24);*/
        /*long min=((l/(60*1000))-day*24*60-hour*60);*/
        long min=l/(60*1000);
        return min;
    }

    public static Date strToDate(String date,String format){
        try{
            SimpleDateFormat sdf=new SimpleDateFormat(format);
            return sdf.parse(date);
        }catch (Exception e){
            return null;
        }

    }
    public static String longToStr(long time,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date = new Date(time);
        return sdf.format(date);
    }
    public static Date longToDate(long time,String format) throws Exception{
        return strToDate(longToStr(time,format),format);
    }

    /**
     * 返回当日凌晨时间
     *
     * @return yyyyMMdd
     */
    public static Date getTodayStart(Date date) {
        if(date==null){
            date=new Date();
        }
        return strToDate(dateToStr(date, "yyyy-MM-dd 00:00:00"),"yyyy-MM-dd 00:00:00");
    }

    public static Long dateDiff(String startTime, String endTime,  String format, String str) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            hour = diff % nd / nh + day * 24;// 计算差多少小时
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
            sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + (hour - day * 24) + "小时"
                    + (min - day * 24 * 60) + "分钟" + sec + "秒。");
            System.out.println("hour=" + hour + ",min=" + min);
            if (str.equalsIgnoreCase("h")) {
                return hour;
            } else {
                return min;
            }

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (str.equalsIgnoreCase("h")) {
            return hour;
        } else {
            return min;
        }
    }
    /**
     * 获取第二天凌晨时间
     * @return
     */
    public static Date getNextDayStart(Date date) {

        try {
            if(date==null){
                date=new Date();
            }
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            GregorianCalendar gerCal = new GregorianCalendar();
            gerCal.setTime(date);
            gerCal.add(GregorianCalendar.DATE, 1);
            String nextDay = sdf.format(gerCal.getTime());
            date = sdf.parse(nextDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 日期差计算 例如在某个日期增加几天后的日期 返回几天后日期
     *
     * @param addDate
     * @return
     */
    public static Date getIncreaseDT(int addDate) {
        String strResult = "0000-00-00";
        try {
            Calendar localDate = new GregorianCalendar();
            // 把字符串型日期转换为日期
            Date tempDate = new Date();
            localDate.setTime(tempDate);
            localDate.add(Calendar.DATE, addDate);
            String curyear = "" + localDate.get(Calendar.YEAR);
            int intmonth = localDate.get(Calendar.MONTH) + 1;
            String curmonth = "" + intmonth;
            String curday = "" + localDate.get(Calendar.DAY_OF_MONTH);

            if (curmonth.length() == 1) {
                curmonth = "0" + curmonth;
            }
            if (curday.length() == 1) {
                curday = "0" + curday;
            }
            strResult = curyear + "-" + curmonth + "-" + curday;
        } catch (Exception e) {
            strResult = "";
        }
        return strToDate(strResult,"yyyy-MM-dd");
    }
    /**
     * 获一周的开始日期 （星期天的日期）
     *
     * @return
     */
    public static Date getLastWeek() {
        Date result=getIncreaseDT(-7);
        return result;
    }

    /**
     * 获取上月信息
     */
    public static Date getLastMonth() {
        Date result=getIncreaseDT(-30);
        return result;
    }
    /**
     * 获取上三个月信息
     */
    public static Date getThreeMonth() {
        Date result=getIncreaseDT(-90);
        return result;
    }


    public static boolean afterDate(Date beforDate,Date afterDate){
        return beforDate.after(afterDate);
    }

    /**
     * 判断时间是否在时间区间内
     * @param sourceTime HH:mm-HH:mm
     * @param curTime HH:mm
     * @return
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            }
            else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }

}
